package edu.sctu.boot.admin.generator.vue;

import edu.sctu.boot.admin.generator.utils.DbUtils;
import edu.sctu.boot.admin.generator.utils.FileUtils;
import edu.sctu.boot.admin.generator.utils.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.*;

public class VueGenerator extends BaseGenerator {

    public void execute() {
        appendAPIServer();
        createVueFiles();
    }

    public void appendAPIServer() {
        try {
            File baseServerFile = new File(VueConf.BASE_SERVER_FILE);

            if (!FileUtils.contains(baseServerFile, VueConf.ENTITY_SERVER)) {

                FileWriter baseServerWriter = new FileWriter(baseServerFile, true);
                baseServerWriter.append("\n");
                baseServerWriter.append(VueConf.ENTITY_SERVER);

                baseServerWriter.flush();
                baseServerWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createVueFiles() {

        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("entityUpper", VueConf.ENTITY.toUpperCase(Locale.ROOT));
            map.put("entityLower", VueConf.ENTITY.toLowerCase(Locale.ROOT));
            map.put("entityServer", VueConf.ENTITY + "Server");
            map.put("colMap", getVisibleFields());

            generate("vue/api_biz.vm", VueConf.API_ENTITY, map);
            generate("vue/store_biz.vm", VueConf.STORE_ENTITY, map);
            generate("vue/view_index.vm", VueConf.VIEW_INDEX_FILE, map);
            generate("vue/view_page.vm", VueConf.VIEW_PAGE_FILE, map);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 需要在页面上显示的字段
    private Map<String, Object> getVisibleFields() throws SQLException, ClassNotFoundException {
        Map<String, String> map = DbUtils.getTableFileds(VueConf.TABLE_NAME);

        // 不需要在页面显示的字段列表
        List<String> excludeFieldList = new ArrayList<>();
        excludeFieldList.add("create_user_id");
        excludeFieldList.add("version");
        excludeFieldList.add("is_enabled");
        excludeFieldList.add("create_time");
        excludeFieldList.add("update_time");
        excludeFieldList.add("update_user_id");
        excludeFieldList.add("create_user_id");
        excludeFieldList.add("del_flag");
        excludeFieldList.add("description");
        excludeFieldList.add("id");

        Map<String, Object> resultMap = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {

            String fieldName = entry.getKey();
            if (!excludeFieldList.contains(fieldName)) {

                fieldName = StringUtils.convert(fieldName);
                resultMap.put(fieldName, entry.getValue());
            }
        }

        return resultMap;
    }
}
